package com.usooft.metaTalent.core.system.service.basic;

import com.usooft.framework.core.password.PasswordProvider;
import com.usooft.metaTalent.core.system.entity.Employee;
import com.usooft.metaTalent.core.system.query.EmployeeQuery;
import com.usooft.metaTalent.core.system.service.EmployeeService;
import com.usooft.metaTalent.core.system.service.base.BaseEmployeeService;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.RandomStringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import static com.usooft.framework.core.utils.Checks.*;

@Service
@RequiredArgsConstructor
public class BasicEmployeeService extends BaseEmployeeService implements EmployeeService {

    private final PasswordProvider passwordProvider;

    @Override
    @Transactional
    public Employee createAdmin(String name,
                                String phoneNumber,
                                String password) {
        return internalAdd(phoneNumber, passwordProvider.hash(password), name, "未知", true, false);
    }

    @Override
    public boolean matchPassword(Employee employee, String password) {
        checkNotNull(employee, "必须提供员工");
        checkNotNull(password, "必须提供密码");
        checkNotNull(employee.getPassword(), "账号未设置密码，请先设置密码后再登录");

        return passwordProvider.match(employee.getPassword(), password);
    }

    @Override
    @Transactional
    public String resetPassword(Employee employee) {
        checkNotNull(employee, "必须提供员工");

        String password = RandomStringUtils.randomAlphanumeric(12);
        employee.setPassword(password);
        internalUpdate(employee);

        return password;
    }

    @Override
    @Transactional
    public Employee create(String name, String phoneNumber, String gender, Boolean locked) {
        check(!existsPhoneNumber(phoneNumber), "手机号已存在");
        return internalAdd(name, phoneNumber, null, gender, false, locked);
    }

    private boolean existsPhoneNumber(String phoneNumber) {
        return count(EmployeeQuery.Filter.builder()
                .phoneNumberIs(phoneNumber)
                .build()) > 0;
    }

    @Override
    @Transactional
    public void edit(Employee employee,
                     String name,
                     String phoneNumber,
                     String gender,
                     Boolean locked) {
        checkNotNull(employee, "必须提供员工");
        check(phoneNumber.equals(employee.getPhoneNumber()) || !existsPhoneNumber(phoneNumber), "手机号已存在");

        employee.setName(name);
        employee.setPhoneNumber(phoneNumber);
        employee.setGender(gender);
        employee.setLocked(locked);
        internalUpdate(employee);
    }

    @Override
    @Transactional
    public void lock(Employee employee) {
        employee.setLocked(true);
        internalUpdate(employee);
    }

    @Override
    @Transactional
    public void unlock(Employee employee) {
        employee.setLocked(false);
        internalUpdate(employee);
    }

    @Override
    @Transactional
    public void changePassword(Employee employee, String originalPassword, String newPassword) {
        checkEquals(employee.getPassword(), passwordProvider.hash(originalPassword), "原密码不正确");
        employee.setPassword(passwordProvider.hash(newPassword));
        internalUpdate(employee);
    }

    // region 自动生成
    // endregion
}